package ap1112.ga1.passi;


public class AP1112HS4 {

        /**
         * @param args
         */
        public static void main(String args[]) {

                int[][] bays = new int[4][3];
                int[] cont = { 24, 22, 19, 18, 15, 13, 11, 10, 9, 7, 6, 5 };
                int container = 0;

                while (checkAllFull(bays) == true) { // true = noch nicht voll!
//                      printBay(bays);
                        bays = fillBay(cont[container], bays);

                        container++;
                        printBay(bays);
                }

        }

        // ende main

        public static int addBays(int bays[][], int bay) {
                //gewicht
                int value = 0;
                for (int i = 3; i > 1; i--) {
                        value += bays[i][bay];

                }
                if (bays[0][bay] != 0)
                        value = -1;
               
                return value;

        }

        public static int getLowestBay(int bays[][]) {
                int bay1 = addBays(bays, 0);
                int bay2 = addBays(bays, 1);
                int bay3 = addBays(bays, 2);

                if (bay1 <= bay2 && bay1 <= bay3 && bay1 != -1)
                        return 0;
                if (bay2 <= bay3 && bay2 != -1)
                        return 1;
                return 2;

        }

        public static int[][] fillBay(int container, int bays[][]) {
                int lowest = getLowestBay(bays);
                System.out.println("LOW: " + lowest);

                for (int i = 3; i >= 0; i--) {
                        if (bays[i][lowest] == 0) {
                                bays[i][lowest] = container;
                                break;
                        }
                }
                return bays;

        }

        public static boolean checkAllFull(int[][] bays) {
                boolean flag = false;
                ;
                for (int i = bays[0].length-1; i >= 0; i--) {
                        if (bays[0][i] == 0) {
                                flag = true;
                        }

                }
                return flag;
        }

        public static void printBay(int[][] bays) {
                for (int i = 0; i < 4; i++) {
                        for (int j = 0; j < 3; j++) {
                                System.out.print(bays[i][j] + " ");
                        }
                        System.out.println();
                }

        }
}


